package cn.com.qiancheng.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.qiancheng.service.AddStuService;
import cn.com.qiancheng.service.MD5;

/**
 * Servlet implementation class addStuServlet
 */
public class AddStuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddStuServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String userName = request.getParameter("user");
		String realName = request.getParameter("real");
		String password = request.getParameter("passw");
		String stuClass = request.getParameter("stuclass");
		password = MD5.GetMD5Code(password);
		int i = new AddStuService().validate(userName, realName, password, stuClass);
		if(i==0){
			request.setAttribute("success", "1");
			RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/JSP/addStudent.jsp");
			rd.forward(request, response);
		}else{
			request.setAttribute("success", "2");
			RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/JSP/addStudent.jsp");
			rd.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
